package com.navinfo.tripanalysis.common.arithmetic;

import com.navinfo.tripanalysis.common.arithmetic.annotation.Arithmetic;
import com.navinfo.tripanalysis.common.arithmetic.common.*;
import com.navinfo.tripanalysis.common.arithmetic.convert.EventDataConvert;
import com.navinfo.tripanalysis.common.arithmetic.manager.AbstractArithmetic;
import com.navinfo.tripanalysis.common.config.ConfigMap;
import com.navinfo.tripanalysis.common.protocol.java.EventDataPb;
import com.navinfo.tripanalysis.common.util.ArithmeticUtils;

import java.util.Optional;

/**
 * 东风冷车运行算法<br/>
 * 依赖{@link Mileage}里程油耗算法<br/>
 * 输出：统计、事件<br/>
 * @author web
 */
@Arithmetic(name = "东风冷车运行算法")
public class DrivingCold_DF extends AbstractArithmetic<VarCommonNumber> {
    /**
     * 事件类型
     */
    private EventDataPb.EventType eventType = EventDataPb.EventType.vehicleColdRunning;
    /**
     * #冷车启动大气温度阈值,-2摄氏度
     */
    private int temperatureThreshold = ConfigMap.getInteger("DF_Cold_Running_Tem_Threshold");
    /**
     * 东风冷车运行0到1km/h时长阀值，单位：ms
     */
    private int firstTimeThreshold = ConfigMap.getInteger("DF_Cold_Running_First_Threshold");
    /**
     * 东风冷车运行1到30km/h时长阀值，单位：ms
     */
    private int secondTimeThreshold = ConfigMap.getInteger("DF_Cold_Running_Second_Threshold");

    @Override
    public void calculate() {
        logger.debug("进入东风冷车运行算法.");
        CommonData commonData = getCommonData();
        int atmosphericTemperature = commonData.getAtmosphericTem();

        //当前点满足冷车运行条件，开启事件
        if (commonData.getLastRotation()==0 && commonData.getRotation()>0 && atmosphericTemperature<temperatureThreshold) {
            startEventData(eventType);
            return;
        }

        //冷车运行事件进行中
        OuterEventData eventData = drivingData.getEventData(eventType);
        if(null!=eventData && atmosphericTemperature<temperatureThreshold) {
            int speedKmh = commonData.getSpeedKmh();
            if (speedKmh>=0 && speedKmh<1) {
                //extendField01用来设置0-1冷车运行的时长
                eventData.setExtendField01(Optional.ofNullable(eventData.getExtendField01()).orElse(0) + commonData.getDffTime());
                eventData.setExtendField05(Optional.ofNullable(eventData.getExtendField05()).orElse(0.0) + commonData.getDffDistance());
                eventData.setExtendField06(Optional.ofNullable(eventData.getExtendField06()).orElse(0.0) + commonData.getDffOil());
//                eventData.setExtendField02(0);
//                eventData.setExtendField07(0.0);
//                eventData.setExtendField08(0.0);

            }else if(Optional.ofNullable(eventData.getExtendField01()).orElse(0) < firstTimeThreshold && speedKmh >=1){
                endEventData(eventData);
            }

            if (speedKmh>=0 && speedKmh<30) {

//                eventData.setExtendField01(0);
//                eventData.setExtendField05(0.0);
//                eventData.setExtendField06(0.0);
                //extendField02用来设置1-30冷车运行的时长
                eventData.setExtendField02(Optional.ofNullable(eventData.getExtendField02()).orElse(0) + commonData.getDffTime());
                eventData.setExtendField07(Optional.ofNullable(eventData.getExtendField07()).orElse(0.0) + commonData.getDffDistance());
                eventData.setExtendField08(Optional.ofNullable(eventData.getExtendField08()).orElse(0.0) + commonData.getDffOil());

                if (Optional.ofNullable(eventData.getExtendField02()).orElse(0) >= secondTimeThreshold) {
                    drivingData.removeEventData(eventType);
                }
            }else if(Optional.ofNullable(eventData.getExtendField02()).orElse(0) < secondTimeThreshold && speedKmh > 30) {
                endEventData(eventData);
            }else if(Optional.ofNullable(eventData.getExtendField02()).orElse(0) >= secondTimeThreshold){
                drivingData.removeEventData(eventType);
            }
//            }else{
//                endEventData(eventData);
//            }
        }
        //事件结束，清除正在缓冲的事件
        else {
            Optional.ofNullable(eventData).ifPresent(e ->{
                drivingData.removeEventData(e.getEvent());
            });
        }

        logger.debug("东风冷车运行算法结束.");
    }

    @Override
    public void collect(OuterStatisticData statisticDataOut) {
        VarCommonNumber obj = getObj();
        statisticDataOut.setColdRunningMileage(ArithmeticUtils.mileageMToM(obj.getMileage()));
        statisticDataOut.setColdRunningFuel(ArithmeticUtils.fuelMlToMl(obj.getFuel()));
        statisticDataOut.setColdRunningDuration(ArithmeticUtils.timeMsToSecond(obj.getDuration()));
        statisticDataOut.setColdRunningNumber(obj.getNumber());
    }

    /**
     * 结束事件处理
     * @param eventData
     */
    private void endEventData(OuterEventData eventData){
        EventDataConvert.convertEnd(eventData, drivingData);

        //累加到统计变量中
        VarCommonNumber var = getObj();
        var.addNumber(1);
        if(Optional.ofNullable(eventData.getExtendField01()).orElse(0) > 0){
            var.addMileage(Optional.ofNullable(eventData.getExtendField05()).orElse(0.0));
            var.addFuel(Optional.ofNullable(eventData.getExtendField06()).orElse(0.0));
            var.addDuration(Optional.ofNullable(eventData.getExtendField01()).orElse(0));
        }else{
            var.addMileage(Optional.ofNullable(eventData.getExtendField07()).orElse(0.0));
            var.addFuel(Optional.ofNullable(eventData.getExtendField08()).orElse(0.0));
            var.addDuration(Optional.ofNullable(eventData.getExtendField02()).orElse(0));
        }

        //放入输出事件列表
        drivingData.addEventDataToList(eventData);

        //删除当前缓存的事件
        drivingData.removeEventData(eventType);
    }
}
